package com.jin10.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jin10.bean.BaseResponse;
import com.jin10.entity.Config;
import com.jin10.service.IConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 文章配置信息保存
 * </p>
 *
 * @author raofy
 * @since 2022-03-09
 */
@RestController
@RequestMapping("/config")
public class ConfigController {

    @Autowired
    IConfigService iConfigService;

    @PostMapping("saveOrUpdate")
    public BaseResponse save(@RequestBody Config dto) {
        if (Objects.isNull(dto.getId())) {
            dto.setTitleId(1);
            return BaseResponse.ok(iConfigService.save(dto));
        } else {
            return BaseResponse.ok(iConfigService.updateById(dto));
        }
    }

    @GetMapping("get")
    public BaseResponse get(@RequestParam(value = "id", defaultValue = "1") Integer id) {
        List<Config> list = iConfigService.list(new LambdaQueryWrapper<Config>().orderByDesc(Config::getId));
        if (list == null || list.isEmpty()) {
            return BaseResponse.ok(new Config());
        } else {
            return BaseResponse.ok(list.get(0));

        }
    }
}
